package per.turtle.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.common.core.web.page.TableDataInfo;
import per.turtle.common.log.annotation.Log;
import per.turtle.common.log.enums.BusinessType;
import per.turtle.common.security.annotation.PreAuthorize;
import per.turtle.system.entity.SysNotice;
import per.turtle.system.service.ISysNoticeService;

import java.util.List;

/**
 * @author turtle
 */
@RequestMapping("notice")
@RequiredArgsConstructor
@RestController
@Tag(name = "通知公告管理", description = "notice_通知公告管理模块")
public class SysNoticeController extends BaseController {

    private final ISysNoticeService noticeService;

    @Operation(summary = "新增通知公告", description = "新增通知公告")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    @PreAuthorize(hasPermission = "system:notice:add")
    public CommonResult<Void> add(@Validated @RequestBody SysNotice notice) {
        return result(noticeService.insertNotice(notice));
    }

    @Operation(summary = "修改通知公告", description = "修改通知公告")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermission = "system:notice:edit")
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody SysNotice notice) {
        return result(noticeService.updateNotice(notice));
    }

    @Operation(summary = "根据通知公告编号获取详细信息", description = "根据通知公告编号获取详细信息")
    @GetMapping("{noticeId}")
    @PreAuthorize(hasPermission = "system:notice:query")
    public CommonResult<SysNotice> getInfo(@PathVariable Long noticeId) {
        return CommonResult.success(noticeService.selectNoticeById(noticeId));
    }

    @Operation(summary = "获取通知公告列表或分页", description = "获取通知公告列表或分页（区别于是否传入分页参数）")
    @PostMapping("list")
    @PreAuthorize(hasPermission = "system:notice:list")
    public TableDataInfo<SysNotice> list(@RequestBody SysNotice notice) {
        List<SysNotice> list = startPage(s -> noticeService.selectNoticeList(notice));
        return getDataTable(list);
    }

    @Operation(summary = "删除通知公告", description = "删除通知公告")
    @DeleteMapping("{noticeIds}")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermission = "system:notice:remove")
    public CommonResult<Void> remove(@PathVariable List<Long> noticeIds) {
        return result(noticeService.deleteNoticeByIds(noticeIds));
    }

}
